﻿using System;
using Artem.Google.UI;
using BikeBouncer.BusinessLayer;
using BikeBouncer.BusinessLayer.Entities;
using BikeBouncer.Site.Enums;

namespace BikeBouncer.Site
{
	public partial class BoxTrackItem_aspx : BaseBoxPage
	{
		#region Page methods

		protected void Page_Load(object sender, EventArgs e)
		{
			Int32 bikeId = GetIdFromQuery("bike");

			if (bikeId < 1)
			{
				AppendError(GetLocalResourceObject("ErrorInvalidBike").ToString());
				return;
			}

			if (!Page.IsPostBack)
			{
				Item item = new Item(bikeId);
				Note note = new Note(DataAccess.Note_Get(item, item.StatusDate, 3));

				InitMap();

				LoadPolice(note);
				LoadBlackMarkets(note);
			}
		}

		#endregion

		#region Load to map

		private void InitMap()
		{
			Maps.EnableReverseGeocoding = true; 
			Maps.EnableScrollWheelZoom = true;
			Maps.Latitude = 51.5;
			Maps.Longitude = 0.15;
			Maps.Zoom = 9;

			Maps.Address = SessionManager.CurrentCountry;
		}

		private void LoadPolice(Note note)
		{
			String cacheKey = "Police-City-" + note.Address.City.Id;
			Object cache = GetCache(cacheKey);
			PoliceCollection PoliceOffices;

			if (cache == null)
			{
				PoliceOffices = new PoliceCollection(DataAccess.Police_GetByCity(note.Address.City.Id));

				SetCache("cacheKey", PoliceOffices, CacheDuration.Long);
			}
			else
			{
				PoliceOffices = (PoliceCollection)cache;
			}

			foreach (Police p in PoliceOffices)
			{
				GoogleMarker marker;

				if (p.Address.Latitude != 0 && p.Address.Longitude != 0)
				{
					marker = new GoogleMarker(p.Address.Latitude, p.Address.Longitude);
				}
				else
				{
					marker = new GoogleMarker(p.Address.ToString(", "));
				}

				marker.Title = p.Title;
				marker.InfoContent.CssClass = "MapInfoWindow";
				marker.Text = "<h3>" + p.Title + "</h3><p>" + p.Address.ToString("<br />") + "</p>";
				marker.Icon.Url = "/Images/Ico/MarkerPolice.png";
				marker.Icon.Size = new GoogleSize(25, 25);

				Maps.Markers.Add(marker);
			}
		}

		private void LoadBlackMarkets(Note note)
		{
			String cacheKey = "BlackMarket-City-" + note.Address.City.Id;
			Object cache = GetCache(cacheKey);
			BlackMarketCollection BlackMarkets;

			if (cache == null)
			{
				BlackMarkets = new BlackMarketCollection(DataAccess.BlackMarket_GetByCity(note.Address.City.Id));

				SetCache("cacheKey", BlackMarkets, CacheDuration.Long);
			}
			else
			{
				BlackMarkets = (BlackMarketCollection)cache;
			}

			foreach (BlackMarket bm in BlackMarkets)
			{
				GoogleMarker marker;

				if (bm.Address.Latitude != 0 && bm.Address.Longitude != 0)
				{
					marker = new GoogleMarker(bm.Address.Latitude, bm.Address.Longitude);
				}
				else
				{
					marker = new GoogleMarker(bm.Address.ToString(", "));
				}

				marker.Title = bm.Title;
				marker.InfoContent.CssClass = "MapInfoWindow";
				marker.Text = "<h3>" + bm.Title + "</h3><p>" + bm.Address.ToString("<br />") + "</p>";
				marker.Icon.Url = "/Images/Ico/MarkerBlackMarket.png";
				marker.Icon.Size = new GoogleSize(25, 25);

				Maps.Markers.Add(marker);
			}
		}

		#endregion
	}
}